package org.jeecg.modules.demo.report.controller;

import cn.afterturn.easypoi.entity.vo.MapExcelConstants;
import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.view.PoiBaseView;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.report.entity.ReconciliationReport;
import org.jeecg.modules.demo.report.service.IReconciliationReportService;
import org.jeecg.modules.demo.report1.entity.Columns;
import org.jeecg.modules.demo.report1.entity.FreeType;
import org.jeecg.modules.demo.report1.mapper.GjshYingshouMapper;
import org.jeecg.modules.demo.report1.service.IGjshYingshouService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

@Controller
public class Daochu extends JeecgController<ReconciliationReport, IReconciliationReportService>{
    @Autowired
    private GjshYingshouMapper gjshYingshouMapper;
    @Autowired
    private IGjshYingshouService gjshYingshouService;
    @GetMapping("/report/reconciliationReport/download")
    public void download(HttpServletRequest request,HttpServletResponse response,ReconciliationReport reconciliationReport) {
        //当前社区的全部表头
        List<FreeType> columnTableList = gjshYingshouMapper.selectColumns(reconciliationReport.getCommunityName());
        //获取动态列
        List<Columns> columns = gjshYingshouService.getColumns(reconciliationReport.getCommunityName(), columnTableList);
        //获取动态数据
        List<Map<String,Object>> resultDataSource = gjshYingshouService.getResultDataSource(reconciliationReport.getCommunityName(), columnTableList, columns);
        //计算表列下的合计
        Map<String,Object> bigDecimalList = gjshYingshouService.getResultTotal(resultDataSource, columnTableList);
        //设置表头列表
        List<ExcelExportEntity> entity = new ArrayList<>();
        //遍历动态列集合
        for (Columns column : columns) {
            // 创建 ExcelExportEntity 对象，并设置表头和字段名
            ExcelExportEntity exportEntity = new ExcelExportEntity(column.getTitle(), column.getDataIndex());

            // 设置列宽度，例如设置固定宽度为 20
            exportEntity.setWidth(20);

            // 将设置好的 ExcelExportEntity 对象添加到列表中
            entity.add(exportEntity);
        }
        //添加合计行
        //添加合计数据
        Set<String> keys = bigDecimalList.keySet();
        //添加合计行
        Map<String,Object> item = new HashMap<>();
        item.put("communityName_dictText","合计");
        //遍历合计数据
        for (String key : keys) {
            Object value = bigDecimalList.get(key);
            item.put(key,value);
        }
        resultDataSource.add(item);

        ModelMap modelMap = new ModelMap();
        ExportParams params = new ExportParams("2412312", "测试", ExcelType.XSSF);
        params.setFreezeCol(2);
        modelMap.put(MapExcelConstants.MAP_LIST, resultDataSource);
        modelMap.put(MapExcelConstants.ENTITY_LIST, entity);
        modelMap.put(MapExcelConstants.PARAMS, params);
        modelMap.put(MapExcelConstants.FILE_NAME, "EasypoiMapExcelViewTest");
        PoiBaseView.render(modelMap, request, response, MapExcelConstants.EASYPOI_MAP_EXCEL_VIEW);
    }

}
